package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.SpecialService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 决策专刊管理
 */
@RestController
@RequestMapping("/bk")
public class SpecialController {

    private static final Logger logger = LogManager.getLogger(SpecialController.class);
    
    @Resource
    private SpecialService specialService;

    @Resource
    private ArticleSearchService articleSearchService;

    @LoginRequired
    @PostMapping("/addSpecial")
    @ApiOperation(value = "新增决策专刊")
    public JsonResult addSpecial(
            @ApiParam(value = "决策专刊标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "决策专刊副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "专刊名称") @RequestParam(value = "name",required = false) String name,
            @ApiParam(value = "期数") @RequestParam(value = "period",required = false) String period,
            @ApiParam(value = "摘要") @RequestParam(value = "summary",required = false) String summary,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "年份") @RequestParam(value = "year",required = false) String year,
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "顺序") @RequestParam(value = "order",required = false) String order
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("name", name);
            pageData.put("period", period);
            pageData.put("summary", summary);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("year", year);
            pageData.put("order", order);
            pageData.put("imgUrl", imgUrl);
            pageData.put("img", imgUrl);

            specialService.addSpecial(pageData);

            try {
                pageData.put("tableName","t_special");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateSpecial")
    @ApiOperation(value = "更新决策专刊")
    public JsonResult updateSpecial(
            @ApiParam(value = "决策专刊ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "决策专刊标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "决策专刊副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "专刊名称") @RequestParam(value = "name",required = false) String name,
            @ApiParam(value = "期数") @RequestParam(value = "period",required = false) String period,
            @ApiParam(value = "摘要") @RequestParam(value = "summary",required = false) String summary,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "年份") @RequestParam(value = "year",required = false) String year,
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "顺序") @RequestParam(value = "order",required = false) String order
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("imgUrl", imgUrl);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("name", name);
            pageData.put("period", period);
            pageData.put("summary", summary);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("year", year);
            pageData.put("order", order);
            pageData.put("img", imgUrl);
            specialService.updateSpecial(pageData);

            try {
                pageData.put("tableName","t_special");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteSpecial")
    @ApiOperation(value = "删除决策专刊")
    public JsonResult deleteSpecial(
            @ApiParam(value = "决策专刊ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            specialService.deleteSpecial(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_special");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableSpecial")
    @ApiOperation(value = "禁用或启用决策专刊")
    public JsonResult disableOrEnableSpecial(
            @ApiParam(value = "决策专刊ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            specialService.disableOrEnableSpecial(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listSpecial")
    @ApiOperation(value = "获取决策专刊列表")
    public JsonResult listSpecial(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(specialService.listSpecial(pageData));
            result.setResultNum(specialService.listSpecialCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addSpecialType")
    @ApiOperation(value = "新增决策专刊类型")
    public JsonResult addSpecialType(
            @ApiParam(value = "决策专刊类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "决策专刊类型父类型ID", required = true) @RequestParam("fatherTypeId") String fatherTypeId,
            @ApiParam(value = "决策专刊类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);
            pageData.put("fatherTypeId", fatherTypeId);

            specialService.addSpecialType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateSpecialType")
    @ApiOperation(value = "更新决策专刊类型")
    public JsonResult updateSpecialType(
            @ApiParam(value = "决策专刊类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "决策专刊类型父类型ID", required = true) @RequestParam("fatherTypeId") String fatherTypeId,
            @ApiParam(value = "决策专刊类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "决策专刊类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("fatherTypeId", fatherTypeId);
            pageData.put("remark", remark);

            specialService.updateSpecialType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteSpecialType")
    @ApiOperation(value = "删除决策专刊类型")
    public JsonResult deleteSpecialType(
            @ApiParam(value = "决策专刊类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            specialService.deleteSpecialType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableSpecialType")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableSpecialType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            specialService.disableOrEnableSpecialType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listSpecialType")
    @ApiOperation(value = "获取决策专刊类型列表")
    public JsonResult listSpecialType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(specialService.listSpecialType(pageData));
            result.setResultNum(specialService.listSpecialTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listSpecialTypeAll")
    @ApiOperation(value = "获取决策专刊类型列表所有")
    public JsonResult listSpecialTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(specialService.listSpecialTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addSpecialTypeFather")
    @ApiOperation(value = "新增决策专刊类型父类型")
    public JsonResult addSpecialTypeFather(
            @ApiParam(value = "决策专刊类型父类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "决策专刊类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);

            specialService.addSpecialTypeFather(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateSpecialTypeFather")
    @ApiOperation(value = "更新决策专刊类型父类型")
    public JsonResult updateSpecialTypeFather(
            @ApiParam(value = "决策专刊类型父类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "决策专刊类型父类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "决策专刊类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("remark", remark);

            specialService.updateSpecialTypeFather(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteSpecialTypeFather")
    @ApiOperation(value = "删除决策专刊类型父类型")
    public JsonResult deleteSpecialTypeFather(
            @ApiParam(value = "决策专刊类型父类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            specialService.deleteSpecialTypeFather(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableSpecialTypeFather")
    @ApiOperation(value = "禁用或启用决策专刊类型父类型")
    public JsonResult disableOrEnableSpecialTypeFather(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            specialService.disableOrEnableSpecialTypeFather(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listSpecialTypeFather")
    @ApiOperation(value = "获取决策专刊类型父类型列表")
    public JsonResult listSpecialTypeFather(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(specialService.listSpecialTypeFather(pageData));
            result.setResultNum(specialService.listSpecialTypeFatherCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listSpecialTypeFatherAll")
    @ApiOperation(value = "获取决策专刊类型父类型列表所有")
    public JsonResult listSpecialTypeFatherAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(specialService.listSpecialTypeFatherAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
